package cn.sc.summer.rabbitmq.config;

import cn.sc.summer.constant.rabbit.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类名：死信队列配置
 *
 * @author a-xin
 * @date 2024/2/5 10:51
 */
@Configuration
@ConditionalOnClass(value = RabbitConfig.class)
public class DeadExchangeConfig extends RabbitConfig {

    @Bean(RabbitConstant.DEAD_EXCHANGE)
    public DirectExchange deadExchange() {
        return ExchangeBuilder
                .directExchange(RabbitConstant.DEAD_EXCHANGE)
                .durable(true)
                .build();
    }

    @Bean(RabbitConstant.DEAD_QUEUE)
    public Queue deadExchangeQueue() {
        Queue queue = QueueBuilder.durable(RabbitConstant.DEAD_QUEUE).build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }

    @Bean(RabbitConstant.DEAD_EXCHANGE_BINDING)
    public Binding deadExchangeBinding() {
        return BindingBuilder.bind(deadExchangeQueue()).to(deadExchange()).with(RabbitConstant.DEAD_EXCHANGE_ROUTING_KEY);
    }

}
